package io.gitee.zicai.stream.test

import io.gitee.zicai.stream.send.MsgSender
import org.junit.runner.RunWith
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import java.util.concurrent.TimeUnit
import kotlin.test.Test

/**
 * StreamTest
 * @author zicai
 * @since 2020-05-11
 */
@RunWith(SpringRunner::class)
@SpringBootTest
open class StreamTest {

    @Autowired
    private lateinit var sender: MsgSender

    /**
     * 普通队列
     */
    @Test
    open fun testSend() {

        (1..10).forEach { i ->
            try {
                TimeUnit.SECONDS.sleep(i.toLong())
            } catch (e: Exception) {
            }
            sender.send(i)
        }
    }

    /**
     * 延时队列，可以用来做定时任务
     */
    @Test
    open fun testSendDelay() {

        (1..10).forEach { i ->
            try {
                TimeUnit.SECONDS.sleep(i.toLong())
            } catch (e: Exception) {
            }
            sender.sendDelayMsg(i)
        }

        TimeUnit.SECONDS.sleep(15)
    }

    @Autowired
    private lateinit var rabbitMessagingTemplate: RabbitMessagingTemplate

    @Test
    open fun testAmqp() {
        rabbitMessagingTemplate.convertAndSend("amqp-test-queue", "msg from amqp" as Any, mutableMapOf("type" to "a").toMap())
    }
}